package org.jincheng.ch06;

import org.junit.Test;

import java.lang.reflect.*;
import java.util.ArrayList;

/**
 * @author Jane
 * @date 2024-11-27 9:47
 */

public class DemoRefClass {

    @Test  //获取类
    public void testClass() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //Role role =null;// new Role();

        //反射
        //获取类  1 ,2,3
        Class role1 =null;// new Role().getClass();
        Class role2 = Role.class;
        Class role3 = Class.forName("org.jincheng.ch06.Role");

        Role role4= (Role) role3.newInstance();

        String name = role1.getName();
        System.out.println(name);

        String simpleName = role2.getSimpleName();
        System.out.println(simpleName);

        Package role3Package = role3.getPackage();
        System.out.println(role3Package.getName());

        Class superclass = role3.getSuperclass();
        System.out.println(superclass);

        System.out.println(role4);
    }
    @Test  //获取接口
    public void testInterface(){

       Class clz1 =ArrayList.class;

       Class[] interfaces = clz1.getInterfaces();

        for (int i = 0; i < interfaces.length; i++) {

            Class clz =interfaces[i];
            System.out.println(clz.getName());

        }

    }
    @Test  //获取修饰符
    public void testModifiers() throws InstantiationException, IllegalAccessException, NoSuchMethodException, ClassNotFoundException, InvocationTargetException {
        Class clz1 = Class.forName("org.jincheng.ch06.Role");//Role.class;

        Constructor c1 = clz1.getDeclaredConstructor();
        c1.setAccessible(true);

        Role o = (Role)c1.newInstance();
        System.out.println(o);

//        int modifiers = clz1.getModifiers();
//        System.out.println(modifiers);
    }
    @Test  //获取访问修饰符
    public void getField() throws NoSuchFieldException {
//        Field[] fields=Role.class.getFields();
        Field[] fields = Role.class.getDeclaredFields();

        Field createTime = Role.class.getDeclaredField("createTime");

        System.out.println("---createTime:"+createTime.getName());

        for (int i = 0; i < fields.length; i++) {
            System.out.println(fields[i].getName());
            //System.out.println(fields[i].getModifiers() & Modifier.STATIC);

            if ((fields[i].getModifiers() & Modifier.STATIC)== Modifier.STATIC){
                System.out.println("有static");
            }
            if ((fields[i].getModifiers() & Modifier.PUBLIC)== Modifier.PUBLIC){
                System.out.println("有public");
            }

            if ((fields[i].getModifiers() & Modifier.PRIVATE)== Modifier.PRIVATE){
                System.out.println("private");
            }else{
                System.out.println("没有abstract");
            }

            System.out.println("----------------------");
        }
    }

    @Test  //获取构造函数
    public void getConstract(){
        Class clz = Role.class;

        Constructor[] constructors = clz.getConstructors();
        for (int i = 0; i < constructors.length; i++) {

//            Class[] types = constructors[i].getParameterTypes();
//
//            for (int j = 0; j < types.length; j++) {
//                System.out.print(types[j].getName()+" ");
//            }
            Parameter[] parameters = constructors[i].getParameters();
            for (int j = 0; j < parameters.length; j++) {
                System.out.println(parameters[j].getName() + "\t" + parameters[j].getType());
            }
            System.out.println();

        }

    }

    @Test  //获取方法信息
    public void getMethod(){
        Class clz  = Role.class;
        Method[] methods = clz.getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method m = methods[i];
            Class<?> aClass = m.getDeclaringClass();
            System.out.println(aClass.getName()+"."+m.getName());

            Class<?>[] exceptionTypes = m.getExceptionTypes();
            for (Class<?> exceptionType : exceptionTypes) {
                System.out.println("\t"+exceptionType.getName());
            }

        }
    }
}
